package com.hhkj.admin.bank.common.component.supplier;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.bank.domain.Bank;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.bank.model.res.BankRes;
import com.hhkj.admin.bank.service.base.BankRuleService;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.admin.question.service.extend.QuestionExtendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * BankBizUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/25 14:21
 */
@Component
public class BankSupplier
{

    @Autowired
    private QuestionExtendService questionExtendService;
    @Autowired
    private BankRuleService bankRuleService;


    /**
     * 给题库补全规则信息
     *
     * @param bank 题库信息
     */
    public void supplyRule(Bank bank)
    {
        // 获取题目规则信息
        BankRule bankRule = bankRuleService.getById(bank.getRuleId());
        bank.setRule(bankRule);
    }

    /**
     * 给题库补全题目列表信息
     *
     * @param resList 题库信息列表
     */
    public void supplyQuestionList(List<BankRes> resList)
    {
        // 获取各个题目的题库信息
        List<Long> bankIdList = resList.stream().map(BankRes::getId).collect(Collectors.toList());
        Map<Long, List<QuestionRes>> questionListGroupByBankId
                = questionExtendService.listResGroupByBankIdList(bankIdList)
                .stream()
                .collect(Collectors.groupingBy(Question::getBankId));
        for (BankRes res : resList) {
            if (questionListGroupByBankId.containsKey(res.getId())) {
                res.setQuestionGroupList(questionListGroupByBankId.get(res.getId()));
            }
        }
    }

    /**
     * 给题库补全题目列表信息
     *
     * @param bank 题库信息
     */
    public void supplyQuestionList(BankRes bank)
    {
        if (ObjectUtil.isNull(bank) || ObjectUtil.isNull(bank.getId())) {
            return;
        }
        List<QuestionRes> questionGroupList = questionExtendService.listPoGroupByBankId(bank.getId());
        bank.setQuestionGroupList(questionGroupList);
    }
}
